<?php
	$model_dir =  ((!defined('SHOP_DEVELOPER') || !constant('SHOP_DEVELOPER')) && version_compare(PHP_VERSION,'5.0','>=')?'model_v5':'model');

	if (!class_exists('mdl_goods'))
	{
		require(CORE_DIR.'/'.$model_dir.'/trading/mdl.goods.php');
	}

	class mdl_collocGoods extends mdl_goods
	{
		function getCollocItemsDetail($goods_id)
		{
			$col = 'goods_id,image_default,thumbnail_pic,mktprice,price,name,store,spec,pdt_desc,spec_desc';
			$goods = $this->db->selectrow('SELECT '.$col.' FROM sdb_goods WHERE goods_id='.intval($goods_id));
			if(!$goods) return false;
			$goods['spec'] = unserialize($goods['spec']);
			if($goods['pdt_desc'] = unserialize($goods['pdt_desc'])){    //如果是多物品商品
				$s = array_keys($goods['pdt_desc']);
				asort($s);
				foreach($this->db->select('SELECT * FROM sdb_products WHERE product_id in ('.implode(',',$s).') AND goods_id='.$goods_id.' ORDER BY product_id') as $product){
					$product['props'] =  unserialize($product['props']);
					foreach($product['props']['spec'] as $k => $v){
						$product['props']['spec'][$k] = stripslashes(trim($v)); //review: stripslashes有意义？
						$product['props']['spec_value_id'][$k] = stripslashes(trim($product['props']['spec_value_id'][$k]));
						$product['props']['spec_private_value_id'][$k] = stripslashes(trim($product['props']['spec_private_value_id'][$k]));
					}
	//                foreach($product['props'] as $pk => $pv){
	//                    $product['props'][$pk] =  array_reverse($pv , true );
	//                }
					$product = array_merge($product, $this->getMemberPrice($goods_id, $product['product_id'], $levelid));
					$product['sale_price'] = $product['price'];
					if($levelid && isset($product['mprice'][$levelid])){
						$product['price'] = $product['mprice'][$levelid];
					}
					$goods['products'][] = $product;
					/*
					foreach($product['props']['spec'] as $k=>$v){
						$goods['specinfo'][$k][$v] = 1;
					}
					*/
				}
			}else{
				$aRet = $this->db->selectrow('SELECT product_id,bn,props, store, freez FROM sdb_products WHERE goods_id='.intval($goods_id));
				if($aRet){
					$goods['product_id'] = $aRet['product_id'];
					$goods['product_bn'] = $aRet['bn'];
					$goods['store'] = $aRet['store'];
					$goods['freez'] = $aRet['freez'];
					$goods['props'] = unserialize($aRet['props']);
					$goods = array_merge($goods, $this->getMemberPrice($goods_id, $goods['product_id']));
					$goods['sale_price'] = $goods['price'];
					if($levelid && isset($goods['mprice'][$levelid])){
						$goods['price'] = $goods['mprice'][$levelid];
					}
				}
			}

			//新规格
			$goods['spec_desc_str'] = urlencode($goods['spec_desc'] );
			$spec_desc=unserialize($goods['spec_desc']);
			$goods['spec_desc'] = $spec_desc;
			$tmpGoods=array();
			$specDefaultPic = $this->system->getConf('spec.default.pic');
			if (is_array($spec_desc)){
				$specValue = &$this->system->loadModel('goods/specification');
				foreach($spec_desc as $key => $val){
					$tmpRow = $specValue->getFieldById($key,array('spec_name','spec_type','spec_show_type'));
					$tmpGoods['spec_desc'][$key]['name']=$tmpRow['spec_name'];
					foreach($val as $k=>$v){
						if (empty($val[$k]['spec_image'])||is_null($val[$k]['spec_image'])){
							$tValue=$specValue->getValueById($val[$k]['spec_value_id'],array('spec_image'));
							$val[$k]['spec_image'] = $tValue['spec_image'];
						}
						if (empty($val[$k]['spec_image'])||is_null($val[$k]['spec_image'])){
							$val[$k]['spec_image'] = $specDefaultPic;
						}
						$goods['spec_desc'][$key][$k]['spec_type'] = $tmpRow['spec_type'];
						$val[$k]['spec_type'] = $tmpRow['spec_type'];
						$val[$k]['spec_goods_images']=$key."@".$v['spec_goods_images']."|".$k;
						$spec_type = $tmpRow['spec_type'];
					}
					$tmpGoods['spec_desc'][$key]['value']=$val;
					if ($tmpRow['spec_show_type']=="select"){
						$tmpSelGoods['spec_desc'][$key]['name'] =$tmpRow['spec_name'];
						$tmpSelGoods['spec_desc'][$key]['spec_type'] = $spec_type;
						$tmpSelGoods['spec_desc'][$key]['value']=$val;
					}
					elseif ($tmpRow['spec_show_type']=="flat"){
						$tmpFlatGoods['spec_desc'][$key]['name'] =$tmpRow['spec_name'];
						$tmpFlatGoods['spec_desc'][$key]['spec_type']=$spec_type;
						$tmpFlatGoods['spec_desc'][$key]['value']=$val;
					}
				}
			}
			$goods['specVdesc'] = $tmpGoods['spec_desc'];
			$goods['FlatSpec'] = $tmpFlatGoods['spec_desc'];
			$goods['SelSpec'] = $tmpSelGoods['spec_desc'];
			$product2spec = array();
			$spec2product = array();
			$cur=&$this->system->loadModel('system/cur');
			foreach( $goods['products'] as $pid =>$pitem ){
				if($pitem['marketable'] == 'false')
					continue;
				$price=$cur->get_cur_money($pitem['price'],'');
				foreach($pitem['mprice'] as $key=>$value){
					$mprice[$key]=$cur->get_cur_money($value,'');
				}

				$product2spec[$pitem['product_id']] = array(
					'bn'=>$pitem['bn'],
					'price'=>$price,
					'mktprice'=>$cur->get_cur_money($pitem['mktprice'],''),
	//                'cost'=>$pitem['cost'],
					'store'=>($pitem['store'] === null?'9999':($pitem['store']-$pitem['freez'])),
					'weight'=>$pitem['weight'],
					'mprice'=>$mprice
				);
				$apvid = array();
				foreach( $pitem['props']['spec_private_value_id'] as $sid => $pvid ){
					$apvid[] = $pvid;
					$spec2product[$pvid]['product_id'][] = $pitem['product_id'];
					$spec2product[$pvid]['images'] = json_decode( '['.$goods['spec_desc'][$sid][$pvid]['spec_goods_images'].']' );
					if (is_array($goods['FlatSpec'][$sid]['value'][$pvid]))
						$goods['FlatSpec'][$sid]['value'][$pvid]['display']="block";
					if (is_array($goods['SelSpec'][$sid]['value'][$pvid]))
						$goods['SelSpec'][$sid]['value'][$pvid]['display']="block";
				}
				$product2spec[$pitem['product_id']]['spec_private_value_id'] = json_decode('["'.implode('","',$apvid).'"]');
			}
			$goods['product2spec'] = $product2spec;
			$goods['spec2product'] = $spec2product;
			//-----
			return $goods;
		}
	}

?>